<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");
%>
<html>
<head>
<META http-equiv=Content-Type content="text/html; charset=UTF-8">
<!--LINK href="images/css.css" type=text/css rel=stylesheet-->
<style type="text/css">
<!--
.STYLE1 {font-size: 12px}
-->
</style>
</head>
<%
    String driver = "oracle.jdbc.driver.OracleDriver";
    String connectionURL = "jdbc:oracle:thin:@oracle.students.itee.uq.edu.au:1521:iteeo"; 
    Connection conn = null;
    Statement st = null;
    ResultSet rs = null;
    List pollid = new ArrayList();
    List pollname=new ArrayList();

    try{
        Class.forName(driver); 
    } catch(java.lang.ClassNotFoundException e) {
        e.printStackTrace();
    }
    try {
        conn = DriverManager.getConnection(connectionURL,"CSSE3004GE","967431"); 
        st=conn.createStatement();
        rs=st.executeQuery("SELECT distinct Poll_Question.PollID, Poll.Name FROM Poll_Question, Poll where Poll.ID=Poll_Question.PollID order by PollID asc");
        while (rs.next())
        {
            pollid.add(rs.getString(1));
            pollname.add(rs.getString(2));
        }
        rs.close();
        st.close();
        conn.close();
    }  catch (Exception e)  {   
        e.printStackTrace();
    }
%>
<body>
<p>
  <script language="javascript">
	var XMLHttpReq;
    var currentSort;
    function createXMLHttpRequest() {
        if(window.XMLHttpRequest) { 
            XMLHttpReq = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) { 
            try {
                XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
    }	
    
    function sendRequest(url) {
        createXMLHttpRequest();
        XMLHttpReq.open("GET", url, true);
        XMLHttpReq.onreadystatechange = processResponse;
        XMLHttpReq.send(null);  
    }

    function processResponse() {
        if (XMLHttpReq.readyState == 4) {
            if (XMLHttpReq.status == 200) { 
                updateMenu();
            } else { 
                  alert("Request failed.");
            }
        }
    }

    function updateMenu() {
        var res=XMLHttpReq.responseXML.getElementsByTagName("res")
        var questionidrecord = document.all.questionidrecord;
        questionidrecord.options.length=0;
        questionidrecord.add(new Option("QuestionID",""));
        for(var i=0;i<res.length;i++){
        	questionidrecord.add(new Option(res[i].firstChild.data,res[i].firstChild.data));
        }
    }

    function showSubMenu(obj) {
        sendRequest("maqstnjsp.jsp?sort=" + obj);
    }
    function sendRedirect(){
		var pid=pollidrecord.options[pollidrecord.selectedIndex].text;
		var qid=questionidrecord.options[questionidrecord.selectedIndex].text;
		var ctype=charttype.options[charttype.selectedIndex].text;
        if(pid=="PollID"){
            alert("Please select a poll ID")
            return;
            }
        if(qid=="QuestionID"){
            alert("Please select a question ID")
            return;
            }
        switch (ctype){
        case "Barchart3D Vertical":
            window.open("/DBP/showchart/bar3dv.jsp?pollid="+pid+"&questionid="+qid);
            break;
        case "Barchart Vertical":
            window.open("/DBP/showchart/barv.jsp?pollid="+pid+"&questionid="+qid);
            break;
        case "Barchart3D Horizontal":
            window.open("/DBP/showchart/bar3dh.jsp?pollid="+pid+"&questionid="+qid);
            break;
        case "Barchart Horizontal":
            window.open("/DBP/showchart/barh.jsp?pollid="+pid+"&questionid="+qid);
            break;
        case "Piechart3D":
            window.open("/DBP/showchart/pie3d.jsp?pollid="+pid+"&questionid="+qid);
            break;
        case "Piechart":
            window.open("/DBP/showchart/pie.jsp?pollid="+pid+"&questionid="+qid);
            break;
        }
    }
</script>
&nbsp;&nbsp;&nbsp;<span class="STYLE2">Poll ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Question ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Chart Type</span></p>
<p>
  <select onChange="showSubMenu(this.options[this.options.selectedIndex].value)" name="pollidrecord"  style="width:80px">
    <option value=''>-Select Poll-</option>
    <%
    for(int i=0;i<pollid.size();i++)
    {
        out.println("<option value='"+pollid.get(i)+"'>"+pollid.get(i)+"    "+ pollname.get(i)+"</option>");
    }
%>
  </select>
  <select name="questionidrecord" style="width:100px">
    <option name="">QuestionID</option>
  </select> 
  <!-- <input type="checkbox" name="questionidrecord"><br> -->
  <select name="charttype" onChange="sendRedirect() ">
  	<option name="">ChartType</option>
    <option value ="a">Barchart3D Vertical</option>
    <option value ="b">Barchart Vertical</option>
    <option value ="c">Barchart3D Horizontal</option>
     <option value ="d">Barchart Horizontal</option>
     <option value="e">Piechart3D</option>
     <option value="f">Piechart</option>
  </select>
</p>

  
</body>
</html>